<template>
  <el-dialog :visible.sync="visible" :title="!dataForm.configureId ? $t('add') : $t('update')" :close-on-click-modal="false" :close-on-press-escape="false">
    <el-form :model="dataForm" :rules="dataRule" ref="dataForm" @keyup.enter.native="dataFormSubmitHandle()" :label-width="$i18n.locale === 'en-US' ? '120px' : '80px'">
          <el-form-item label="投票详情ID" prop="voteId">
          <el-input v-model="dataForm.voteId" placeholder="投票详情ID"></el-input>
      </el-form-item>
          <el-form-item label="投票配置标题" prop="configureName">
          <el-input v-model="dataForm.configureName" placeholder="投票配置标题"></el-input>
      </el-form-item>
          <el-form-item label="投票配置内容" prop="configureContent">
          <el-input v-model="dataForm.configureContent" placeholder="投票配置内容"></el-input>
      </el-form-item>
          <el-form-item label="配置项投票数" prop="voteCount">
          <el-input v-model="dataForm.voteCount" placeholder="配置项投票数"></el-input>
      </el-form-item>
      <el-form-item label="参赛人员图片" prop="configureUrl">
        <el-upload :action="action" list-type="picture-card" ref="upload"  :limit="1"
          :on-success="uploadSuccess" :on-remove="handleRemove"   :on-preview="handlePictureCardPreview">
          <i class="el-icon-plus"></i>
        </el-upload>
        <el-dialog :visible.sync="dialogVisible">
          <img width="100%" :src="dialogImageUrl" alt="">
        </el-dialog>
      </el-form-item>
          <!-- <el-form-item label="状态  0：停用   1：正常" prop="status">
          <el-input v-model="dataForm.status" placeholder="状态  0：停用   1：正常"></el-input>
      </el-form-item> -->
              <!-- <el-form-item label="更新者" prop="updater">
          <el-input v-model="dataForm.updater" placeholder="更新者"></el-input>
      </el-form-item>
          <el-form-item label="更新时间" prop="updateDate">
          <el-input v-model="dataForm.updateDate" placeholder="更新时间"></el-input>
      </el-form-item> -->
          <el-form-item label="编号" prop="configureNumber">
          <el-input v-model="dataForm.configureNumber" placeholder="编号"></el-input>
      </el-form-item>
      </el-form>
    <template slot="footer">
      <el-button @click="visible = false">{{ $t('cancel') }}</el-button>
      <el-button type="primary" @click="dataFormSubmitHandle()">{{ $t('confirm') }}</el-button>
    </template>
  </el-dialog>
</template>

<script>
import Cookies from 'js-cookie'
import debounce from 'lodash/debounce'
export default {
  data () {
    return {
      visible: false,
      dataForm: {
        configureId: '',
        voteId: '',
        configureName: '',
        configureContent: '',
        voteCount: '',
        status: '',
        creator: '',
        createDate: '',
        updater: '',
        updateDate: '',
        configureNumber: '',
        configureUrl:''
      },
      dialogImageUrl: '',
      dialogVisible: false
    }
  },
  computed: {
    dataRule () {
      return {
        voteId: [
          { required: true, message: this.$t('validate.required'), trigger: 'blur' }
        ],
        configureName: [
          { required: true, message: this.$t('validate.required'), trigger: 'blur' }
        ],
        configureContent: [
          { required: true, message: this.$t('validate.required'), trigger: 'blur' }
        ],
        voteCount: [
          { required: true, message: this.$t('validate.required'), trigger: 'blur' }
        ],
        status: [
          { required: true, message: this.$t('validate.required'), trigger: 'blur' }
        ],
        updater: [
          { required: true, message: this.$t('validate.required'), trigger: 'blur' }
        ],
        updateDate: [
          { required: true, message: this.$t('validate.required'), trigger: 'blur' }
        ],
        configureNumber: [
          { required: true, message: this.$t('validate.required'), trigger: 'blur' }
        ],
        configureUrl: [
          { required: true, message: this.$t('validate.required'), trigger: 'blur' }
        ]
      }
    }
  },
  methods: {
    init () {
      this.action = `${window.SITE_CONFIG['apiURL']}/sys/oss/upload?token=${Cookies.get('token')}`
      this.visible = true
      this.$nextTick(() => {
        this.$refs['dataForm'].resetFields()
        if (this.dataForm.configureId) {
          this.getInfo()
        }
      })
    },
    // 获取信息
    getInfo () {
      this.$http.get(`/we/wevoteconfigure/${this.dataForm.configureId}`).then(({ data: res }) => {
        if (res.code !== 0) {
          return this.$message.error(res.msg)
        }
        this.dataForm = {
          ...this.dataForm,
          ...res.data
        }
      }).catch(() => {})
    },
    // 表单提交
    dataFormSubmitHandle: debounce(function () {
      this.$refs['dataForm'].validate((valid) => {
        if (!valid) {
          return false
        }
        this.$http[!this.dataForm.configureId ? 'post' : 'put']('/we/wevoteconfigure/', this.dataForm).then(({ data: res }) => {
          if (res.code !== 0) {
            return this.$message.error(res.msg)
          }
          this.$message({
            message: this.$t('prompt.success'),
            type: 'success',
            duration: 500,
            onClose: () => {
              this.visible = false
              this.$emit('refreshDataList')
            }
          })
        }).catch(() => {})
      })
    }, 1000, { 'leading': true, 'trailing': false }),
    handleRemove(file, fileList) {
      this.dataForm.configureUrl = '';
      },
      handlePictureCardPreview(file) {
        this.dialogImageUrl = file.url;
        this.dialogVisible = true;
      },
    uploadSuccess(response, file, fileList) {
      this.dataForm.configureUrl = response.data.src;
      console.log(response)
    }
  }
}
</script>
